package larpplanner.gui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.cloudgarden.resource.SWTResourceManager;

public class LoginWin extends org.eclipse.swt.widgets.Composite {
	
	private GUISafeLARPManager mngr = GUISafeLARPManager.get();
	
	private Label emailLbl;
	private Text passTxt;
	private Button okBtn;
	private Button newBtn;
	private Label emptyLbl;
	private Button cancelBtn;
	private Text emailTxt;
	private Label passLbl;
	
	{
        //Register as a resource user - SWTResourceManager will
        //handle the obtaining and disposing of resources
        SWTResourceManager.registerResourceUser(this);
    }
	
	public LoginWin(org.eclipse.swt.widgets.Composite parent, int style) {
		super(parent, style);
		initGUI();
		initLogic();
	}
	
	private void okHandler() {
		String mail = emailTxt.getText();
		String pass = passTxt.getText();
		if(mail.equals("") || pass.equals(""))
			return;
		
		// TODO: replace with DB handler function
		if(mail.equals("admin") && pass.equals("admin")) {
			getShell().close();
			Display display = Display.getDefault();
			Shell shell = new Shell(display);
			AdminWin adminWin = new AdminWin(shell, SWT.NONE);
			adminWin.open();
			return;
		}
			
		// try to sign in the user
		mngr.signIn(mail, pass, new SafeCallback<Long>() {
			public void onReturn(Long userID) {
		        if(userID != null) {
		            getShell().close();
		            Display display = Display.getDefault();
		            Shell shell = new Shell(display);
		            QuestSelectionWin questSelWin = new QuestSelectionWin(shell, SWT.NONE, userID);
		            questSelWin.open();
		        }
		        else{
		            PopMessage.errorMessage("bad email or password");
		            passTxt.setText("");
		        }
		    }
		});
	}
	
	private void initLogic() {
		okBtn.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				okHandler();
			}
		});
		
		passTxt.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				if(e.keyCode == 13) //13 = enter
					okHandler();
			}
		});
		
		emailTxt.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				if(e.keyCode == 13) //13 = enter
					okHandler();
			}
		});
		
		newBtn.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				SignUpWin signWin = new SignUpWin(getShell(), SWT.NONE);
				signWin.open();
			}
		});
		
		cancelBtn.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				emailTxt.setText("");
				passTxt.setText("");
			}
		});
	}
	
	public static void openLoginWin() {
        Display display = Display.getDefault();
        Shell shell     = new Shell(display);
        LoginWin inst   = new LoginWin(shell, SWT.NULL);
        Point size      = inst.getSize();
        
        shell.setLayout(new FillLayout());
        shell.setText("LARP Planner Login");
        shell.layout();
        
        if(size.x == 0 && size.y == 0) {
            inst.pack();
            shell.pack();
        } else {
            Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y);
            shell.setSize(shellBounds.width, shellBounds.height);
        }
        
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
    }
	
	private void initGUI() {
		try {
			GridLayout thisLayout = new GridLayout();
			thisLayout.numColumns = 4;
			thisLayout.marginTop = 15;
			thisLayout.marginBottom = 15;
			thisLayout.marginLeft = 6;
			thisLayout.marginRight = 6;
			this.setLayout(thisLayout);
			this.setSize(258, 100);
			{
				emailLbl = new Label(this, SWT.BORDER);
				GridData userLblLData = new GridData();
				userLblLData.horizontalAlignment = GridData.FILL;
				emailLbl.setLayoutData(userLblLData);
				emailLbl.setText("email:");
			}
			{
				GridData userTxtLData = new GridData();
				userTxtLData.grabExcessHorizontalSpace = true;
				userTxtLData.horizontalAlignment = GridData.FILL;
				userTxtLData.heightHint = 15;
				userTxtLData.horizontalSpan = 3;
				emailTxt = new Text(this, SWT.SINGLE);
				emailTxt.setTextLimit(30);
				emailTxt.setLayoutData(userTxtLData);
			}
			{
				passLbl = new Label(this, SWT.BORDER);
				GridData passLblLData = new GridData();
				passLblLData.horizontalAlignment = GridData.FILL;
				passLbl.setLayoutData(passLblLData);
				passLbl.setText("pass:");
			}
			{
				GridData passTxtLData = new GridData();
				passTxtLData.grabExcessHorizontalSpace = true;
				passTxtLData.heightHint = 15;
				passTxtLData.horizontalAlignment = GridData.FILL;
				passTxtLData.horizontalSpan = 3;
				passTxt = new Text(this, SWT.SINGLE | SWT.PASSWORD);
				passTxt.setTextLimit(30);
				passTxt.setLayoutData(passTxtLData);
			}
			{
				GridData emptyLblLData = new GridData();
				emptyLbl = new Label(this, SWT.NONE);
				emptyLbl.setLayoutData(emptyLblLData);
			}
			{
				okBtn = new Button(this, SWT.PUSH | SWT.CENTER);
				okBtn.setText("OK");
			}
			{
				cancelBtn = new Button(this, SWT.PUSH | SWT.CENTER);
				GridData cancelBtnLData = new GridData();
				cancelBtn.setLayoutData(cancelBtnLData);
				cancelBtn.setText("Clear");
			}
			{
				newBtn = new Button(this, SWT.PUSH | SWT.CENTER);
				GridData newBtnLData = new GridData();
				newBtn.setLayoutData(newBtnLData);
				newBtn.setText("Sign Up");
			}
			this.layout();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
}
